Skip to content

fix: Treat ^ as exponentation in DuckDB - #2972

Open
nuno-faria wants to merge 1 commit into
apache:mainfrom
nuno-faria:fix_exponentiation_duckdb
Open

nuno-faria wants to merge 1 commit into
apache:mainfrom
nuno-faria:fix_exponentiation_duckdb

Conversation

@nuno-faria

Copy link
Copy Markdown
Contributor

Dialects: DuckDB

Closes: #2971

Like Postgres, ^ should be represented as exponentiation in DuckDB: https://duckdb.org/docs/2.0/sql/functions/numeric

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.28%. Comparing base (96a5132) to head (6c9e0f8).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2972   +/-   ##
=======================================
  Coverage   83.28%   83.28%           
=======================================
  Files          42       42           
  Lines       35355    35355           
  Branches    35355    35355           
=======================================
  Hits        29446    29446           
  Misses       2458     2458           
  Partials     3451     3451           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@LucaCappelletti94
LucaCappelletti94 self-requested a review November 15, 2026 14:24

@LucaCappelletti94 LucaCappelletti94 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few notes, besides in the ones in the code:

  1. Add the fuzzer seeds and actually run the fuzzers
  2. You should give ^ its own precedence above * in DuckDB, so as to handle the precedence error illustrated in the test.

Comment thread src/parser/mod.rs
// In PostgreSQL and DuckDB, ^ stands for the exponentiation operation.
// See https://www.postgresql.org/docs/current/functions-math.html,
// https://duckdb.org/docs/2.0/sql/functions/numeric.
if dialect_is!(dialect is PostgreSqlDialect | DuckDbDialect) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a new method to the Dialect trait instead of extending the use of the dialect_is macro.

Comment thread tests/sqlparser_duckdb.rs
Comment on lines +980 to +988
#[test]
fn test_exponentiation() {
let expected_expr = Expr::BinaryOp {
left: Box::new(Expr::Identifier(Ident::new("a"))),
op: BinaryOperator::PGExp,
right: Box::new(Expr::Identifier(Ident::new("b"))),
};
assert_eq!(duckdb().verified_expr("a ^ b"), expected_expr);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should assert the trees that depend on precedence, because a ^ b alone passes while a * b ^ c parses as (a * b) ^ c.

Suggested change
#[test]
fn test_exponentiation() {
let expected_expr = Expr::BinaryOp {
left: Box::new(Expr::Identifier(Ident::new("a"))),
op: BinaryOperator::PGExp,
right: Box::new(Expr::Identifier(Ident::new("b"))),
};
assert_eq!(duckdb().verified_expr("a ^ b"), expected_expr);
}
#[test]
fn test_exponentiation() {
use BinaryOperator::{Multiply, PGExp, Plus};
let ident = |name| Box::new(Expr::Identifier(Ident::new(name)));
let binary = |left, op, right| Box::new(Expr::BinaryOp { left, op, right });
let minus = |expr| Box::new(Expr::UnaryOp { op: UnaryOperator::Minus, expr });
for (sql, expected) in [
("a * b ^ c", binary(ident("a"), Multiply, binary(ident("b"), PGExp, ident("c")))),
("a + b ^ c", binary(ident("a"), Plus, binary(ident("b"), PGExp, ident("c")))),
("-a ^ b", binary(minus(ident("a")), PGExp, ident("b"))),
("a ^ b ^ c", binary(binary(ident("a"), PGExp, ident("b")), PGExp, ident("c"))),
] {
assert_eq!(duckdb().verified_expr(sql), *expected, "{sql}");
}
}

@nuno-faria

Copy link
Copy Markdown
Contributor Author

Thanks @LucaCappelletti94.

("-a ^ b", binary(minus(ident("a")), PGExp, ident("b"))),

I don't think there is a way to make the negation work here. I searched around and it appears it is already being worked on: #2829

Add the fuzzer seeds and actually run the fuzzers

This part is not clear to me. Is the fuzzer test to check the precedence bug? If so do we need to create a fuzz_duckdb_expr?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DuckDB dialect treats ^ as XOR instead of exponentation

3 participants